package com.cnfangmao.masterdata.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.cnfangmao.masterdata.model.Permission;
import com.cnfangmao.masterdata.model.Role;
import com.cnfangmao.masterdata.model.RolePermission;
import com.cnfangmao.masterdata.common.utils.MD5Utils;
import com.cnfangmao.masterdata.dao.PermissionDao;
import com.cnfangmao.masterdata.dao.RoleDao;
import com.cnfangmao.masterdata.dao.RolePermissionDao;
import com.cnfangmao.masterdata.dto.PermissionDto;
import com.cnfangmao.masterdata.dto.RoleDto;
import com.cnfangmao.masterdata.dto.RolePermissionDto;
import com.cnfangmao.masterdata.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-17
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role> implements RoleService {

	private final Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);

	@Autowired
	private RoleDao roleDao;

	@Autowired
	private RolePermissionDao rolePermissionDao;

	@Autowired
	private PermissionDao permissionDao;

	@Override
	public boolean save(RoleDto role, String createdBy) {
		boolean result = false;
		try {
			Role entityRole = new Role();
			entityRole.setRoleName(role.getRoleName());
			entityRole.setRoleCode(MD5Utils.getMD516(
					String.valueOf(System.currentTimeMillis()
							+ role.getRoleName())).toUpperCase());
			entityRole.setRoleId(UUID.randomUUID().toString());
			entityRole.setRemark(role.getRemark());
			entityRole.setCreatedOn(new Date());
			entityRole.setCreatedBy(createdBy);
			result = roleDao.insert(entityRole) > 0;
			if (result) {
				List<String> rolePermission = role.getRolePermission();
				for (String id : rolePermission) {
					RolePermission rolePermissionEntity = new RolePermission();
					rolePermissionEntity.setRoleId(entityRole.getRoleId());
					rolePermissionEntity.setPermissionId(id);
					rolePermissionEntity.setRolePermissionId(UUID.randomUUID()
							.toString());
					rolePermissionEntity.setCreatedOn(new Date());
					rolePermissionEntity.setCreatedBy(createdBy);
					rolePermissionDao.insert(rolePermissionEntity);
				}

			}
		} catch (Exception ex) {
			log.error("RoleServiceImpl save err : ", ex);
		}

		return result;
	}

	@Override
	public boolean update(RoleDto dto, String modifiedBy) {
		boolean result = false;
		try {
			Role role = roleDao.selectById(dto.getRoleId());
			role.setRoleName(role.getRoleName());
			role.setRemark(role.getRemark());
			role.setCreatedOn(new Date());
			role.setCreatedBy(modifiedBy);
			result = roleDao.updateById(role) > 0;
			if (result) {
				List<String> rolePermission = dto.getRolePermission();
				rolePermissionDao.deleteRolePermission(role.getRoleId());
				for (String id : rolePermission) {
					RolePermission rolePermissionEntity = new RolePermission();
					rolePermissionEntity.setRoleId(role.getRoleId());
					rolePermissionEntity.setPermissionId(id);
					rolePermissionEntity.setRolePermissionId(UUID.randomUUID()
							.toString());
					rolePermissionEntity.setCreatedOn(new Date());
					rolePermissionEntity.setCreatedBy(modifiedBy);
					rolePermissionDao.insert(rolePermissionEntity);
				}
			}
		} catch (Exception ex) {
			log.error("RoleServiceImpl update err : ", ex);
		}

		return result;
	}

	@Override
	public boolean delete(String roleId) {
		boolean result = false;
		try {
			result = roleDao.deleteById(roleId) > 0;
			if (result) {
				rolePermissionDao.deleteRolePermission(roleId);
			}
		} catch (Exception ex) {
			log.error("RoleServiceImpl delete err : ", ex);
		}

		return result;
	}

	@Override
	public IPage<Role> queryListRole(String roleName, int pageNo, int pageSize) {
		IPage<Role> page = roleDao.selectPage(new Page<>(pageNo, pageSize),
				new QueryWrapper<Role>().like(StringUtils.isNotEmpty(roleName),
						"role_name", roleName));

		return page;
	}

	@Override
	public List<PermissionDto> getAllPermissions() {

		List<PermissionDto> permissionList = new ArrayList<PermissionDto>();
		List<Permission> entityList = permissionDao.findAllPermission();
		try {
		for (Permission entity : entityList) {
			PermissionDto permissionDto = new PermissionDto();

			permissionDto.setPermissionId(entity.getPermissionId());
			permissionDto.setPermissionCode(entity.getPermissionCode());
			permissionDto.setPermissionName(entity.getPermissionName());
			permissionDto.setActionPath(entity.getActionPath());
			permissionDto.setRemark(entity.getRemark());
			permissionList.add(permissionDto);

		}
		} catch (Exception ex) {
			log.error("RoleServiceImpl getAllPermissions err : ", ex);
		}
		return permissionList;

	}

	@Override
	public List<RolePermissionDto> getRolePermission(String roleId) {
		List<RolePermission> entityList= rolePermissionDao.findRolePermissionByRoleId(roleId);
		List<RolePermissionDto> rolePermissionList = new ArrayList<RolePermissionDto>();
		try{
		for (RolePermission entity : entityList) {
			RolePermissionDto dto = new RolePermissionDto();
			dto.setRoleId(entity.getRoleId());
			dto.setPermissionId(entity.getPermissionId());
			rolePermissionList.add(dto);

		}
		} catch (Exception ex) {
			log.error("RoleServiceImpl getRolePermission err : ", ex);
		}
		 return rolePermissionList;
	}


	@Override
	public List<Role> getAllRoles() {
		List<Role> rolePermissionList = new ArrayList<Role>();
		try {
			rolePermissionList=roleDao.getAllRoles();
		} catch (Exception ex) {
			log.error("RoleServiceImpl getAllRoles err : ", ex);
		}
		return rolePermissionList;
	}

	@Override
	public Role findByRoleName(String roleName) {
		Role role = null;
		try {
			role = roleDao.findByRoleName(roleName);
		} catch (Exception ex) {
			log.info("RoleServiceImpl findByRoleName :", ex);
		}
		return role;
	}

}
